#include<bits/stdc++.h>
using namespace std;
inline int max(int a,int b){
    return a>b?a:b;
}inline int min(int a,int b){
    return a<b?a:b;
}
int n,m,now,a[300007],b[300007],c[300007],logk[300007];
int d[300007],e[300007];
char ch[20][200007];
bool vis[100007];
int ansn=0;
void dfs(int x,int sum,int p,int fk){
    //cout<<sum<<endl;
    if(x>sum){
        for(int i=1;i<=sum;++i)d[i]=i;
        int s=0;
        while(sum>1){
            ++s;
            for(int i=1;i<=sum;i+=2){
                int r=now-p+s;
                if(ch[r][(i+1)/2]=='0'){
                    d[(i+1)/2]=(e[d[i]]>=r?d[i]:d[i+1]);
                }else{
                    d[(i+1)/2]=(e[d[i+1]]>=r?d[i+1]:d[i]);
                }cout<<d[(i+1)/2]<<" ";
            }sum/=2;
//            for(int i=1;i<=sum+3;++i)cout<<d[i]<<" ";
            //cout<<endl;
        }if(!vis[d[1]])cout<<d[1]<<" ",ansn+=d[1];
//        for(int i=1;i<=x-1;++i)cout<<e[i]<<" ";
//        cout<<endl;
        vis[d[1]]=1;
        return;
    }for(int i=0;i<=4;++i){
        e[x]=i;
        dfs(x+1,sum,p,fk);
    }
}
int X[1001];
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;++i)cin>>a[i];
    for(int i=1;i<=m;++i)cin>>c[i];
    string s;
    now=0;
    memset(logk,0,sizeof logk);
    logk[1]=0;
    for(int i=2;i<=200001;++i)logk[i]=logk[i-1]+!(i&i-1);
    for(int i=1;i<=logk[n]+1;++i){
        cin>>s;
        ++now;
        for(int i=0;i<s.size();++i)ch[now][i+1]=s[i];
;
    }logk[1]=0;
    int Ts;cin>>Ts;
    while(Ts--){
        cin>>X[0]>>X[1]>>X[2]>>X[3];
        for(int i=1;i<=n;++i)b[i]=a[i]^X[i%4];
        int ans=0;
        for(int i=1;i<=m;++i){
            memset(vis,0,sizeof vis);
            ansn=0;
            for(int j=1;j<=c[i];++j)e[j]=b[j];
            dfs(c[i]+1,pow(2,logk[c[i]-1]+1),logk[c[i]],c[i]);
            cout<<ansn<<"\n";

            ans^=(i*ansn);
            //cout<<i;
        }cout<<ans<<"\n";
    }
    return 0;
}
